package com.csdn.leon.fragment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.oschina.app.widget.togglebutton.ToggleButton;
import net.oschina.app.widget.togglebutton.ToggleButton.OnToggleChanged;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.csdn.leon.activity.AboutActivity;
import com.csdn.leon.activity.BlogCloumnListActivity;
import com.csdn.leon.activity.FavoriteActivity;
import com.csdn.leon.activity.WatchBloggerActivity;
import com.csdn.leon.base.MyApplication;
import com.csdn.leon.fragment.FindFragment.MyOnPageChangeListener;
import com.csdn.leon.util.Constants;
import com.csdn.leon.util.FileUtil;
import com.csdn.leon.util.SPUtils;
import com.csdn.leon.view.dialog.CustomDialog;
import com.csdn.leon.view.dialog.CustomDialog.DialogListener;
import com.csdn.leon.view.dialog.DialogHelper;
import com.csdn.leon.view.viewpager.AutoScrollViewpager;
import com.csdn.leon.view.viewpager.ImagePagerAdapter;
import com.csdn.leon.base.R;

public class SettingFragment extends BaseFragment implements DialogListener {

	private Activity activity;
	private View view;

	private ToggleButton mTbLoadImg;
	private TextView mTvCacheSize;
	private TextView mTvExit;
	
	private TextView aboutTv;
	private ToggleButton mTbDoubleClickExit;
	CustomDialog dialog;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity = activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);// 保留fragment对象
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_settings_layout, container,
				false);
		initView();
		initViewData();
		return view;
	}

	private void initView() {
		mTbLoadImg = (ToggleButton) view.findViewById(R.id.tb_loading_img);
		mTvCacheSize = (TextView) view.findViewById(R.id.tv_cache_size);
		mTvExit = (TextView) view.findViewById(R.id.setting_logout);
		mTbDoubleClickExit = (ToggleButton) view
				.findViewById(R.id.tb_double_click_exit);
		mTbLoadImg.setOnClickListener(this);
		mTvCacheSize.setOnClickListener(this);
		mTvExit.setOnClickListener(this);
		mTbDoubleClickExit.setOnClickListener(this);
		view.findViewById(R.id.rl_loading_img).setOnClickListener(this);
		view.findViewById(R.id.rl_notification_settings).setOnClickListener(
				this);
		view.findViewById(R.id.rl_clean_cache).setOnClickListener(this);
		view.findViewById(R.id.rl_double_click_exit).setOnClickListener(this);
		view.findViewById(R.id.rl_about).setOnClickListener(this);
		view.findViewById(R.id.rl_exit).setOnClickListener(this);
		mTbLoadImg.setOnToggleChanged(new OnToggleChanged() {
			@Override
			public void onToggle(boolean on) {
				SPUtils.put(activity, Constants.KEY_LOAD_IMAGE, on);
			}
		});

		mTbDoubleClickExit.setOnToggleChanged(new OnToggleChanged() {
			@Override
			public void onToggle(boolean on) {
				SPUtils.put(activity, Constants.KEY_DOUBLE_CLICK_EXIT, on);
			}
		});
	}

	private void initViewData() {
		if ((boolean) SPUtils.get(activity, Constants.KEY_LOAD_IMAGE, true)) {
			mTbLoadImg.setToggleOn();
		} else {
			mTbLoadImg.setToggleOff();
		}

		if ((boolean) SPUtils.get(activity, Constants.KEY_DOUBLE_CLICK_EXIT,
				true)) {
			mTbDoubleClickExit.setToggleOn();
		} else {
			mTbDoubleClickExit.setToggleOff();
		}
		caculateCacheSize();
	}

	/**
	 * 计算缓存的大小
	 */
	private void caculateCacheSize() {
		long fileSize = 0;
		String cacheSize = "0KB";
		File filesDir = getActivity().getFilesDir();
		File cacheDir = getActivity().getCacheDir();

		fileSize += FileUtil.getDirSize(filesDir);
		fileSize += FileUtil.getDirSize(cacheDir);
		// 2.2版本才有将应用缓存转移到sd卡的功能
		if (MyApplication.isMethodsCompat(android.os.Build.VERSION_CODES.FROYO)) {
			File externalCacheDir = getExternalCacheDir(getActivity());
			fileSize += FileUtil.getDirSize(externalCacheDir);
			// fileSize += FileUtil.getDirSize(new File());
		}
		if (fileSize > 0)
			cacheSize = FileUtil.formatFileSize(fileSize);
		mTvCacheSize.setText(cacheSize);
	}

	@TargetApi(8)
	public File getExternalCacheDir(Context context) {

		// // return context.getExternalCacheDir(); API level 8
		//
		// // e.g. "<sdcard>/Android/data/<package_name>/cache/"
		// final File extCacheDir = new
		// File(Environment.getExternalStorageDirectory(),
		// "/Android/data/" + context.getApplicationInfo().packageName +
		// "/cache/");
		// extCacheDir.mkdirs();
		// return extCacheDir;

		return context.getExternalCacheDir();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_loading_img:
			// Toast.makeText(activity, "点击加载图片", 2000).show();
			mTbLoadImg.toggle();
			break;
		case R.id.rl_notification_settings:
			break;
		case R.id.rl_clean_cache:
			onCleanCache();
			break;
		case R.id.rl_double_click_exit:
			mTbDoubleClickExit.toggle();
			break;
		case R.id.rl_about:
			Intent intent = new Intent(activity,AboutActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_exit:
			break;
		default:
			break;
		}
	}

	public void onCleanCache() {
		if (dialog == null) {
			dialog = DialogHelper.getCustomDialog(activity);
		}
		dialog.setTextContent("是否确认清除缓存");
		dialog.setDialogListener(this);
		dialog.show();
	}

	@Override
	public void onPause() {
		super.onPause();
		// stop auto scroll when onPause
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	public void onStart() {
		super.onStart();
	}

	@Override
	public void isCancel(boolean flag) {
		if (flag) {
			clearAppCache(activity);
		}else{
			
		}
		dialog.cancel();
		
	}

	/**
	 * 清除app缓存
	 * 
	 * @param activity
	 */
	public void clearAppCache(final Activity activity) {
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 1) {
					Toast.makeText(activity, "缓存清除成功", 2000).show();
					mTvCacheSize.setText("0 KB");
				} else {
					Toast.makeText(activity, "缓存清除失败", 2000).show();
				}
			}
		};
		new Thread() {
			@Override
			public void run() {
				Message msg = new Message();
				try {
					MyApplication.getInstance().clearAppCache();
					msg.what = 1;
				} catch (Exception e) {
					e.printStackTrace();
					msg.what = -1;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}

}